import { pddAuth } from '@/api/home.js';

export default {
	middleEllipsis(text, maxLength = 10) {
		if (text.length <= maxLength) return text;
		const half = Math.floor(maxLength / 2);
		return text.slice(0, half) + "..." + text.slice(-half);
	},
	jumpMiiniP(miniPdata, platformName) {
		if (miniPdata.code == 0) {
			if (miniPdata.data.schemaUrl) {
				if (miniPdata.data.schemaUrl.includes('#小程序:')) {
					wx.navigateToMiniProgram({
						shortLink: miniPdata.data.schemaUrl,
					})
				} else {
					const miniPidMap = {
						'拼多多': 'wxa918198f16869201',
						'唯品会': 'wxe9714e742209d35f'
					}
					
					wx.navigateToMiniProgram({
						appId: miniPidMap[platformName],
						path: miniPdata.data.schemaUrl,
					})
				}
			} else if (miniPdata.data.weAppJson) {
				let weAppJsonObj = JSON.parse(miniPdata.data.weAppJson);
				console.log('weAppJsonObj数据：', weAppJsonObj);
				wx.navigateToMiniProgram({
					appId: weAppJsonObj.appId,
					path: weAppJsonObj.pagePath,
				})
			}
		}
		
		if (miniPdata.msg.includes('未授权') && platformName == '拼多多') {
			uni.showToast({
				title: '正在跳转拼多多授权',
				icon: 'none'
			})
			pddAuthFun()
		}
	},
	async pddAuthFun() {
		console.log('拼多多授权');
		let res = await pddAuth();
		console.log('解密拼多多授权响应数据：', res);
		if (res.code == 0) {
			uni.navigateTo({
				url: '/subPages/subWebView/index?url=' + res.data.shortUrl,
			})
		}
	}
}